metro_areas = [
    ('Tokyo', 'JP', 36.933, (35.689722, 139.691667)),
    ('Delhi NCR', 'IN', 21.935, (28.613889, 77.208889)),
    ('Mexico City', 'MX', 20.142, (19.433333, -99.133333)),
    ('New York-Newark', 'US', 20.104, (40.808611, -74.020386)),
    ('São Paulo', 'BR', 19.649, (-23.547778, -46.635833)),
]

def handle_command(message):
    match message:
        case ['BEEPER', frequency, times]:
            print("beep")
        case ['NECK', angle]:
            print("neck")
        case ['LED', ident, intensity]:
            print("LED1")
        case ['LED', ident, red, green, blue]:
            print("LED2")
        case _:
            print("ELSE")


handle_command(["NECK", 123])
# 功能比C的case更强大，和C一样从上到下，但没有fall through
c = '1'
match c:
    case '1':
        print("一")
    case '2':
        print("二")
# 也可以当case用
print(f'{"":15} | {"latitude":>9} | {"longitude":>9}')
for record in metro_areas:
    match record:  # 使用match-case重写上例
        case [name, _, _, (lat, lon)] if lon <= 0:
            print(f'{name:15} | {lat:9.4f} | {lon:9.4f}')
print(f'{"":15} | {"latitude":>9} | {"longitude":>9}')
for record in metro_areas:
    match record:
        case [str(name), _, _, (float() as lat, float() as lon)] if lon <= 0:  # 这里的str和float不是构造函数，而是明确类型！
            print(f'{name:15} | {lat:9.4f} | {lon:9.4f}')
print(f'{"":15} | {"latitude":>9} | {"longitude":>9}')
for record in metro_areas:
    match record:
        case [name, *_, (lat, lon)] if lon <= 0:  # 更强的方法，只接受第一个和倒数两个元素
            print(f'{name:15} | {lat:9.4f} | {lon:9.4f}')


def t_handle(ls):
    match ls:
        case ['define', str(name), _]:
            print("str")
        case ['define', int(value), _]:
            print("int")
        case ['define', value, _]:
            print("define")
        case _:
            print("ELSE")


if __name__ == '__main__':
    t_handle(['define', '123', 15.5])
    t_handle(['define', 123, 15.5])
    t_handle(['define', 12.3, 15.5])
    t_handle([])
